package com.zzc.fwzpxt.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.zzc.fwzpxt.entity.ArticleTag;
import com.zzc.fwzpxt.entity.Tag;
import com.zzc.fwzpxt.mapper.TagMapper;
import com.zzc.fwzpxt.service.IArticleTagService;
import com.zzc.fwzpxt.service.ITagService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author XiaoBingBy
 * @since 2017-10-15
 */
@Service
public class TagServiceImpl extends ServiceImpl<TagMapper, Tag> implements ITagService {

    @Autowired
    private TagMapper tagMapper;
    @Autowired
    private IArticleTagService iArticleTagService;

    @Override
    public List<Tag> findTagsByArticleId(Long id) {
        return tagMapper.findTagsByArticleId(id);
    }

    @Override
    public boolean deleteTagById(List<Tag> tags) {
        tags.forEach(item -> {
            // 删除关联表
            EntityWrapper<ArticleTag> articleTagEntityWrapper = new EntityWrapper<>();
            articleTagEntityWrapper.eq("article_tag_id", item.getId());
            iArticleTagService.delete(articleTagEntityWrapper);
            // 删除标签
            tagMapper.deleteById(item.getId());
        });
        return true;
    }

}
